/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrmQuanlyhoatdong.java
 *
 * Created on Nov 28, 2011, 7:45:56 PM
 */
package GUI;
import DAO.Connect;
import GUI.FrmDetailAdmin;
import DatabaseAccess.quanLyTaiKhoan;
import DatabaseAccess.quanlyHoatdong;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import java.sql.*;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;



/**
 *
 * @author darK.x9
 */
public class FrmQuanlyhoatdong extends javax.swing.JFrame {
    DefaultTableModel model;
    Connection conn;
    Statement st;
    PreparedStatement pstmt;
    ResultSet rs;
    quanlyHoatdong nhap = new quanlyHoatdong();
    Connect connect = new Connect();
    public static String ActiveIDD, ActiveIDE;

    /** Creates new form FrmQuanlyhoatdong */
    public FrmQuanlyhoatdong() {
        initComponents();
         model = (DefaultTableModel) jTableQLHD.getModel();
        nhap.btnLoad(jTableQLHD);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTableQLHD = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        btnAdd = new javax.swing.JToggleButton();
        btnSua = new javax.swing.JToggleButton();
        btnTim = new javax.swing.JToggleButton();
        cmbChon = new javax.swing.JComboBox();
        btnClose = new javax.swing.JButton();
        txtTim = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTableQLHD.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "ActiveID", "ActiveName"
            }
        ));
        jScrollPane1.setViewportView(jTableQLHD);

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 18));
        jLabel1.setForeground(new java.awt.Color(255, 51, 51));
        jLabel1.setText("Quản lý hoạt động của trẻ");

        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/add.png"))); // NOI18N
        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnSua.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/comment.png"))); // NOI18N
        btnSua.setText("Edit");
        btnSua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSuaActionPerformed(evt);
            }
        });

        btnTim.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/search.png"))); // NOI18N
        btnTim.setText("Search");
        btnTim.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTimActionPerformed(evt);
            }
        });

        cmbChon.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Id", "Name" }));

        btnClose.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Exit.jpg"))); // NOI18N
        btnClose.setText("Close");
        btnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCloseActionPerformed(evt);
            }
        });

        txtTim.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtTimActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnAdd)
                        .addGap(87, 87, 87)
                        .addComponent(btnSua)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 97, Short.MAX_VALUE)
                        .addComponent(btnClose))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, 0, 0, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(89, 89, 89)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 229, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(cmbChon, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(txtTim, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(18, 18, 18)
                        .addComponent(btnTim)))
                .addContainerGap(52, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbChon, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtTim, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnTim))
                .addGap(27, 27, 27)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(40, 40, 40)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnClose)
                    .addComponent(btnAdd)
                    .addComponent(btnSua))
                .addGap(39, 39, 39))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
// TODO add your handling code here:
     FrmAddActive frmaddactive = new FrmAddActive();
     frmaddactive.setVisible(true);
}//GEN-LAST:event_btnAddActionPerformed

private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseActionPerformed
// TODO add your handling code here:
    this.dispose();
}//GEN-LAST:event_btnCloseActionPerformed

private void btnTimActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTimActionPerformed
// TODO add your handling code here:
       model.getDataVector().removeAllElements();
    //Cập nhật lại jTable để hiển thị trên màn hình
    jTableQLHD.repaint();
        if (txtTim.getText().length() == 0) {
            JOptionPane.showMessageDialog(this, "Enter a key to Search!");
            return;
        }
        if (txtTim.getText().equals("all")){
            nhap.btnLoad(jTableQLHD);
        }
        if(cmbChon.getSelectedItem().toString().equals("Id")){
            try {                           
                conn = connect.getConnection();
                String strSQL = "select * from tblActivities where ActiveID = ?";
                Vector v = new Vector();
            

                pstmt = conn.prepareStatement(strSQL);
                pstmt.setString(1,txtTim.getText());
                rs = pstmt.executeQuery();
                try {
               
                while (rs.next()) {
                    v = new Vector();
                    for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {

                        v.addElement(rs.getString(i));

                    }
                    model.addRow(v);
                }
                //set lai model cho jtable
                jTableQLHD.setModel(model);
                conn.close();
            }catch (Exception e) {
                e.printStackTrace();
            }
                
            } catch (SQLException ex) {
                ex.printStackTrace();
                JOptionPane.showMessageDialog(this,"Error:"+ex.getMessage());
                return;
            }
        }
         if(cmbChon.getSelectedItem().toString().equals("Name")){
            try {                           
                conn = connect.getConnection();
                String strSQL = "select * from tblActivities where ActiveName like ?";
                Vector v = new Vector();
            

                pstmt = conn.prepareStatement(strSQL);
                pstmt.setString(1,"%"+txtTim.getText()+"%");
                rs = pstmt.executeQuery();
                try {
               
                while (rs.next()) {
                    v = new Vector();
                    for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {

                        v.addElement(rs.getString(i));

                    }
                    model.addRow(v);
                }
                //set lai model cho jtable
                jTableQLHD.setModel(model);
                conn.close();
            }catch (Exception e) {
                e.printStackTrace();
            }
                
            } catch (SQLException ex) {
                ex.printStackTrace();
                JOptionPane.showMessageDialog(this,"Error:"+ex.getMessage());
                return;
            }
        }
    }                                         

    private void txtSearchActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        btnTimActionPerformed(evt);
    }                                         

    private void btnDetailActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
         int n= jTableQLHD.getSelectedRow();
        if(n==-1)
        {
            JOptionPane.showMessageDialog(this,"Choose a Staff to view");
            return;
        }
        ActiveIDD = jTableQLHD.getValueAt(n, 0).toString();
        FrmDetailAdmin frmdetail = new FrmDetailAdmin();
        frmdetail.setVisible(true);
          
           
                
             
    
    
}//GEN-LAST:event_btnTimActionPerformed

private void txtTimActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtTimActionPerformed
// TODO add your handling code here:
    btnTimActionPerformed(evt);
}//GEN-LAST:event_txtTimActionPerformed

private void btnSuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSuaActionPerformed
// TODO add your handling code here:
    int n= jTableQLHD.getSelectedRow();
        if(n==-1)
        {
            JOptionPane.showMessageDialog(this,"Choose a Active to Edit");
            return;
        }
        ActiveIDE = jTableQLHD.getValueAt(n, 0).toString();
        FrmEditActive frmeditactive = new FrmEditActive();
        frmeditactive.setVisible(true);
}//GEN-LAST:event_btnSuaActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrmQuanlyhoatdong.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrmQuanlyhoatdong.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrmQuanlyhoatdong.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrmQuanlyhoatdong.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new FrmQuanlyhoatdong().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToggleButton btnAdd;
    private javax.swing.JButton btnClose;
    private javax.swing.JToggleButton btnSua;
    private javax.swing.JToggleButton btnTim;
    private javax.swing.JComboBox cmbChon;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableQLHD;
    private javax.swing.JTextField txtTim;
    // End of variables declaration//GEN-END:variables
}
